package com.cdfy.framework.web.service;

import com.cdfy.common.core.domain.entity.SysUser;
import com.cdfy.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录校验方法
 * 
 * @author songxl
 */
@Component
public class SysLoginService{

    @Autowired
    private SysUserServiceImpl sysUserService;
    /**
     * 登录验证
     * 
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public String login(String username, String password)
    {
        String str="";
        System.out.println("username:"+username+",password:"+password);
        SysUser sysUser=sysUserService.selectUserByUserName(username);
        if(sysUser!=null){
            str="success";
        }else{
            str="failed";
        }
        //String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        //String captcha = redisCache.getCacheObject(verifyKey);
       // redisCache.deleteUserByIdObject(verifyKey);
//        if (captcha == null)
//        {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
//            throw new CaptchaExpireException();
//        }
//        if (!code.equalsIgnoreCase(captcha))
//        {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
//            throw new CaptchaException();
//        }
        // 用户验证
      //  Authentication authentication = null;
       // try
        //{
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
           // authentication = authenticationManager
                    //.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        //}
        //catch (Exception e)
        //{
//            if (e instanceof BadCredentialsException)
//            {
//                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
//                throw new UserPasswordNotMatchException();
//            }
//            else
//            {
//                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
//                throw new CustomException(e.getMessage());
//            }
        //}
        //AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
       // LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return str;//tokenService.createToken(loginUser)"";
    }
}
